package com.zxl.demo;

import com.zxl.demo.model.User;

import java.io.*;
import java.util.ArrayList;
import javax.servlet.ServletContext;
import javax.servlet.http.*;

public class HelloServlet extends HttpServlet {
    /**
     * 重定向和转发的不同点
     * 页面都会跳转
     * 转发url不发生变化
     * 重定向url发生变化
     */
    private String message = "嘿嘿";

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html");
        //字符编码写在最前面
        response.setHeader("Content-type", "text/html;charset=UTF-8");

        //获取上下文
        ServletContext servletContext = this.getServletContext();
        ArrayList<User> userArrayList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user = new User(i, "" + i, i);
            userArrayList.add(user);
        }


        User user = new User(1, "1", 1);
        servletContext.setAttribute("user",user);

        servletContext.setAttribute("userList",userArrayList);


        response.sendRedirect(request.getContextPath()+"/TestServlet");
    }
}